import { IHeader, IHttpFilter } from "../Interfaces/INetwork";
import { ImpHeader } from "./ImpHeader";


/**
 * Http全局配置
 */
export default class HttpGloableConfig {
    /**请求地址 */
    static baseUrl: string = "http://localhost";
    /**全局响应超时时间，单位毫秒(默认10秒)*/
    static timeout?: number = 10000;
    /**全局请求头信息 */
    static headers: IHeader = new ImpHeader();
    /**全局拦截器 */
    static filter:IHttpFilter; 
    /**
     * 获取请求头信息
     * @param method 请求方式-不同方式读取不通请求头
     * @param customHeader 自定义请求头
     */
    static getHeaders(method, customHeader: any): Array<any> {
        var array = new Array<any>();
        if (method == "get") {
            this.getHeaderArray(array, this.headers.get);
        }
        else if (method == "post") {
            this.getHeaderArray(array, this.headers.post);
        }
        this.getHeaderArray(array, this.headers.common);
        this.getHeaderArray(array, customHeader);
        if (array.length == 0)
            return null;
        else
            return array;
    }

    private static getHeaderArray(haderArray, headerObject: any) {
        if (headerObject) {
            for (const key in headerObject) {
                if (headerObject[key]) {
                    haderArray.push(key );

                    haderArray.push(headerObject[key]);
                }
            }
        }
    }
}

